package com.zhw.mybatis.utils;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcUtils {

    private static Connection conn = null;

    //获取链接
    public static Connection getConn(String url,String user,String password){
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭链接
    public static void close(ResultSet rs, PreparedStatement ptmt, Connection conn){
        try {
            if(rs !=null){
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(ptmt !=null){
                ptmt.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(conn !=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
